import { request,BASEURL} from '../../utils/request'

Page({

  data: {
    show: false,
    num:1,
    BASEURL:'',
    id:1,
    goodsDetail:[],
    contentFilter:''
  },
  onLoad: function (options) {
    this.setData({
      id:options.id
    })
    console.log(this.data.id)
    // 商品详情数据
    this.goodsDetail()
  },

  showPopup() {
   let uid =  wx.getStorageSync('uid')
   if(uid){
    this.setData({ show: true });
   }else{
    wx.navigateTo({
      url: '/pages/login/login',
    })
    return
   }
    
  },

  onClose() {
    this.setData({ show: false });
  },
  // 商品数量加减
  numBtn(e){
    let {index} = e.currentTarget.dataset
    let num = this.data.num
    index==1 ? num++ : num--
    if(num<1) num=1
    if(num>=1) num=1
    this.setData({
      num
    })
  },

  //跳转到支付页面
  async buyPay(){
    let {price} = this.data.goodsDetail
    let {num,id} =this.data
    wx.navigateTo({
      url: '/pages/pay/pay?num='+num+'&id='+id+'&price='+price,
    })
  },
  // 商品详情数据
  async goodsDetail(){
    let id = this.data.id
    let res = await request({
      url: '/api/Wxapp/productInfo',
      method:"POST",
      data:{id}
    })
    console.log(res)
    let goodsDetail = res.data.data
    this.setData({
      goodsDetail,
      BASEURL
    })
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    let {openid} = wx.getStorageSync('uid')
    return {
      title: "邀请好友",
      path:`/pages/index/index?pid=${openid}` 
    }
  }
})